/*
 * LessonMapper 2.Copyright (C) Olivier Motelet.This program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package lessonMapper.query;

import java.rmi.dgc.VMID;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import lessonMapper.lom.LOM;
import lessonMapper.lom.LOMAttribute;
import lor.LOR;

/**
 * LOMQuery is respondible for generating semantically rich queries for
 * retrieving LOM element it is also responsible for ranking the results of this
 * query. Basically, the query is generated based on three main elements: - a
 * keyword query = each keyword must be find in at least one attribute of the
 * resulted LOM - values given to the attributes of the associated LOM = the
 * resulting LOM should match with these values - the restriction values
 * generated by the diffusion framework = the resulting LOM should match with
 * the value range given by the restrictions
 * 
 * Result ranking is based on the suggestion values generated by the diffusion
 * framework. A result obtained a additional point when a attribute value match
 * with one of the suggestion. Better rank is given to results accumulating more
 * points.
 * 
 * @author omotelet
 */

public class LOMQuery {

	/**
	 * 
	 */
	public static final String ITSQueryAttributes = "resources/queryAttributes.xml";

	/**
	 * 
	 */
	public static Vector<LOMAttribute> ITSAttributes = LOMAttribute
			.getAttributeList(LOMQuery.class.getResource(ITSQueryAttributes));

	/**
	 * 
	 */
	protected LOM itsLOM;

	/**
	 * 
	 */
	protected String itsKeywordQuery;

	
	protected String itsID;
	
	
	protected boolean isInit = false;
	/**
	 * create a LOMQuery using he content of general/title keyword and description as keyword query
	 * 
	 * @param aLOM 
	 * @param aKeywordQuery 
	 */
	public LOMQuery(LOM aLOM) {
		itsLOM = aLOM;
		String theKeywordQuery = "";
		theKeywordQuery += " "
				+ LOMAttribute.getLOMAttribute("general/title").getValueIn(
						itsLOM).getValue();
		theKeywordQuery += " "
				+ LOMAttribute.getLOMAttribute("general/keyword").getValueIn(
						itsLOM).getValue();
		theKeywordQuery += " "
				+ LOMAttribute.getLOMAttribute("general/description")
						.getValueIn(itsLOM).getValue();
		theKeywordQuery.replaceAll(",", " ");
		theKeywordQuery.replaceAll(".", " ");
		itsKeywordQuery = theKeywordQuery;
		itsID = new VMID().toString().trim();
		isInit = init();
	}

	
	/**
	 * 
	 * 
	 * @param aLOM 
	 * @param aKeywordQuery 
	 */
	public LOMQuery(LOM aLOM, String aKeywordQuery) {
		itsLOM = aLOM;
		itsKeywordQuery = aKeywordQuery;
		itsID = new VMID().toString().trim();
		isInit = init();
	}

	
	
	/**
	 * register the query on the repository
	 * @return
	 */
    public boolean init(){
    	try {
			LOR.INSTANCE.registerQuery(itsID);
			storeQueryContext(itsLOM, new HashSet<LOM>());
		} catch (Exception e) {
			System.out.println("Problem while initializing the query on the DB");
			//e.printStackTrace();
			return false;
		}
    	return true;
    }
	
    protected void storeQueryContext(LOM aLOM, Set<LOM> aLOMSet) throws Exception{
    	LOR.INSTANCE.putQueryContext(itsID, aLOM.getID(), aLOM.toXMLString());
    	aLOMSet.add(aLOM);
    	for (LOM theLOM: LOM.getRelatedLOM(aLOM))
    		if (!aLOMSet.contains(theLOM))
    			storeQueryContext(theLOM, aLOMSet);
    }

	

    /**
     * return a list of XML LOM result for this query the list is ordered from best to worst score
     * @return
     */
	public List<String> getResults() {
		if (!isInit) init();
		return LOR.INSTANCE.LMQuery(itsID, itsKeywordQuery, itsLOM.getID()); 
	}

	/**
	 * set the chosen result
	 * @param aLOMID
	 */
	public void setChosenResult(String aLOMID){
		if (!isInit) init();
		LOR.INSTANCE.setLMQueryResult(itsID, aLOMID);
	}
	
	/**
	 * release the query (removes it from the repository)
	 */
	public void releaseQuery(){
		LOR.INSTANCE.removeQuery(itsID);
		isInit=false;
	}
	


	
}
